# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
from collections import deque


class Solution:
    def Print(self, p):
        # write code here
        f = True
        a = [(p, 0)]
        d = []
        while a:
            node, level = a.pop(0)
            if len(d) < level + 1:
                d.insert(level, deque())
            if level % 2 == 1:
                d[level].appendleft(node.val)
            else:
                d[level].append(node.val)
            if node.left:
                a.append((node.left, level + 1))
            if node.right:
                a.append((node.right, level + 1))
        return d



pNode1 = TreeNode(8)
pNode2 = TreeNode(6)
pNode3 = TreeNode(10)
pNode4 = TreeNode(5)
pNode5 = TreeNode(7)
pNode6 = TreeNode(9)
pNode7 = TreeNode(11)

pNode1.left = pNode2
pNode1.right = pNode3
pNode2.left = pNode4
pNode2.right = pNode5
pNode3.left = pNode6
pNode3.right = pNode7

S = Solution()
aList = S.Print(pNode1)
print(aList)